package com.anolesoft.yr.mbus.pre.service.impl;

import com.anolesoft.yr.mbus.pre.dto.PreVisitDeleteDTO;
import com.anolesoft.yr.mbus.pre.dto.PreVisitSaveDTO;
import com.anolesoft.yr.mbus.pre.dto.PreVisitUpdateDTO;
import com.anolesoft.yr.mbus.pre.mapper.PreVisitMapper;
import com.anolesoft.yr.mbus.pre.service.PreVisitService;
import com.anolesoft.yr.mbus.pre.vo.PreVisitQueryVO;
import com.anolesoft.yr.mbus.pre.vo.PreVisitQueryVO2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;

@Service
public class PreVisitServiceImpl implements PreVisitService {

    private static final String CODE_EXIST = "CODE_EXIST";
    private static final String SUCCESS = "SUCCESS";
    private static final String EXCEPTION = "EXCEPTION";

    @Autowired
    PreVisitMapper preVisitMapper;

    //新增拜访客户记录
    @Override
    @Transactional
    public String savePreVisit(PreVisitSaveDTO preVisitSaveDTO) throws Exception {
        try {
            int count = 0;

            count = preVisitMapper.queryVisitByCode(preVisitSaveDTO.getVisitCode());
            if (count > 0) {
                return CODE_EXIST;
            }

            int rows = 0;
            rows = preVisitMapper.savePreVisit(preVisitSaveDTO);

            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            throw new TimeoutException(EXCEPTION);
        }
    }

    //修改拜访客户记录
    @Override
    @Transactional
    public String updatePreVisit(PreVisitUpdateDTO preVisitUpdateDTO) throws Exception {
        try {
            int count = 0;

            PreVisitQueryVO2 preVisitQueryVO2 = preVisitMapper.queryPreVisitByUid(preVisitUpdateDTO.getObjuid());
            System.out.println(preVisitUpdateDTO);
            System.out.println(preVisitQueryVO2);
            count = preVisitMapper.queryVisitByCode2(preVisitUpdateDTO.getVisitCode(), preVisitQueryVO2.getUpdateVisitCode());
            if (count > 0) {
                return CODE_EXIST;
            }
            int rows = 0;
            rows = preVisitMapper.updatePreVisit(preVisitUpdateDTO);

            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            throw new TimeoutException(EXCEPTION);
        }
    }

    //删除拜访记录
    @Override
    @Transactional
    public String deletePreVisit(List<PreVisitDeleteDTO> preVisitDeleteDTOList) throws Exception {
        try {
            int rows = 0;
            for (int i = 0; i < preVisitDeleteDTOList.size(); i++) {
                PreVisitDeleteDTO preVisitDeleteDTO = preVisitDeleteDTOList.get(i);
                rows += preVisitMapper.deletePreVisit(preVisitDeleteDTO);
            }

            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            throw new TimeoutException(EXCEPTION);
        }
    }

    //根据主键查询拜访记录唯一数据
    @Override
    public Map<String, Object> queryPreVisitByUid(String uid) throws Exception {
        PreVisitQueryVO2 preVisitQueryVO2 = preVisitMapper.queryPreVisitByUid(uid);
        Map<String, Object> maps = new HashMap<String, Object>();
        maps.put("preVisitQueryVO2", preVisitQueryVO2);
        return maps;
    }

    //查询拜访客户记录
    @Override
    public Map<String, Object> queryPreVisitAll(String visitTime, String clientName, String salesPerson, int page, int limit) throws Exception {
        int count = preVisitMapper.queryPreVisitAllCount(visitTime, clientName, salesPerson);
        List<PreVisitQueryVO> preVisitQueryVOList = preVisitMapper.queryPreVisitAll(visitTime, clientName, salesPerson, (page - 1) * limit, limit);

        Map<String, Object> maps = new HashMap<String, Object>();
        maps.put("count", count);
        maps.put("preVisitQueryVOList", preVisitQueryVOList);
        return maps;
    }

}
